<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<meta http-equiv="Content-Script-Type" content="text/css" />
<title>修改强化剂</title>
<link type="text/css" href="/css/tablecloth.css" rel="stylesheet" media="screen" />
<script type="text/javascript" src="/js/jquery-1.4.4.js"></script>
<script type="text/javascript" src="/js/tablecloth.js"></script>
<script type="text/javascript" src="/js/tableinput.js"></script>

<script type="text/javascript">
function isLessOne(s) 
{ 
//var patrn=/^[0-9]{1,20}$/;
var patrn = /^0+(\.\d+)?$/;
if (!patrn.exec(s)) return false ;
return true ;
}

function isNumber(s)
{
var patrn = /^\d+(\.\d+)?$/; 
if (!patrn.exec(s)) return false ;
return true ;
}

function addFortifier()
{
	var form = document.getElementById("addFortifierForm");
	form.name.value = 
		$("#nameInput").text();
	form.ratio.value = 
		$("#ratioInput").text();
	form.loss.value = 
		$("#lossInput").text();
	form.purity.value = 
		$("#purityInput").text();
	form.cost.value = 
		$("#costInput").text();
	form.nutrimentId.value = 
		$("#nutrimentselector").val();
	form.unit.value = 
		$("#unitselector").val();
	
	if(!isNumber(form.cost.value))
	{
		alert("输入数据错误，请重新出入");
		return;
	}
	if(!isLessOne(form.loss.value))
	{
		alert("输入数据错误，请重新出入");
		return;
	}
	if(!isLessOne(form.ratio.value))
	{
		alert("输入数据错误，请重新出入");
		return;
	}
	if(!isLessOne(form.purity.value))
	{
		alert("输入数据错误，请重新出入");
		return;
	}
	form.submit();
	alert("添加成功");
}

function deleteFortifier(id)
{
	var form = document.getElementById("removeFortifierForm");
	form.id.value = id;
	form.submit();
	alert("删除成功");
}
</script>
</head>


<body>
	<%@ taglib uri="/WEB-INF/tld/list-taglib.tld" prefix="tj" %>
	<form action="/fortifier/addFortifierWithNutriment.do" method="post" id="addFortifierForm">
		<input type="hidden" name="name" />
		<input type="hidden" name="ratio" />
		<input type="hidden" name="loss" />
		<input type="hidden" name="purity" />
		<input type="hidden" name="cost" />
		<input type="hidden" name="nutrimentId" />
		<input type="hidden" name="unit" />
	</form>
    <form action="/fortifier/removeFortifier.do" method="post" id="removeFortifierForm">
    	<input type="hidden" name="id" />
    </form>
	<table id="t" cellspacing="0" cellpadding="0">
		<tr>
			<th class='title1'>强化剂</th>
			<th class='title2'>营养素</th>
			<th class='title3'>单位</th>
			<th class='title4'>比率 </th>
			<th class='title5'>工业损失</th>
			<th class='title6'>纯度 </th>
			<th class='title7'>单价 </th>
			<th class='title8'>添加/删除</th>
		</tr>
		<tr>
			<% out.print("<td id='nameInput' needInput='true'>单击添加</td>"); %>
			<td><tj:list id='nutrimentselector' list='${nutriments}'></tj:list></td>
			<td>
				<select id="unitselector">
					<option>ug</option>
					<option>mg</option>
					<option>g</option>
				</select>
			</td>
			<% out.print("<td id='ratioInput' needInput='true'>单击添加</td>"); %>
			<% out.print("<td id='lossInput' needInput='true'>单击添加</td>"); %>
			<% out.print("<td id='purityInput' needInput='true'>单击添加</td>"); %>
			<% out.print("<td id='costInput' needInput='true'>单击添加</td>"); %>
			<td><input type='button' value='添加' onclick='addFortifier()' /></td>
		</tr>
		<%@ page import="db.fortifier.Fortifier" %>
		<%@ page import="db.nutriment.Nutriment" %>
		<%@ page import="java.util.List" %>
		<%
			@SuppressWarnings("unchecked")
			List<Fortifier> fortifiers = (List<Fortifier>)request.getAttribute("fortifiers");
			for(int i = 0; i < fortifiers.size(); i++) {
				out.print("<tr>");
                out.print("<td>"+fortifiers.get(i).getName()+"</td>");
                if(fortifiers.get(i).getNutriments().size() == 0)
                {
                	out.print("<td>无营养素</td>");
                }
                else
                {
                	out.print("<td>"+((Nutriment)fortifiers.get(i).getNutriments().toArray()[0]).getName()+"</td>");
                }
                if(fortifiers.get(i).getUnit() == null)
                {
                	out.print("<td>无单位</td>");
                }
                else
                {
                	out.print("<td>" + fortifiers.get(i).getUnit() + "</td>");
                }
				out.print("<td>"+fortifiers.get(i).getRatio()+"</td>");
                out.print("<td>"+fortifiers.get(i).getLoss()+"</td>");
                out.print("<td>"+fortifiers.get(i).getPurity()+"</td>");
                out.print("<td>"+fortifiers.get(i).getCost()+"</td>");
                out.print("<td><input type='button' value='删除' onclick = deleteFortifier(" + fortifiers.get(i).getId() + ") /></td></tr>");
				out.print("</tr>");
			}
		%>
	</table>
	<p>&nbsp;</p>
</body>
</html>
